#include "stdio.h"
#include "readTxt.h"
#include <string>
#include <vector>
#define FILENAMEEMPTY 2
#define CONNOTOPENED 4
#define SUCCESS 0
//using namespace std;
readTxt::readTxt(void) : IreadFiler()
{
}

readTxt::~readTxt(void)
{
}

int readTxt::cleaningInvalid(FILE *fp)
{
	char ch;
	while((ch=fgetc(fp))!=EOF)
	{	
		if ( '\r ' == ch || ' \n' == ch || 0 == ch || ' ' == ch)
		{
		}
		else
		{
			break;
		}
	}
	fseek(fp, -1, SEEK_CUR);
	return 0;
}


int readTxt::readFile()
{
	FILE		*fp;
	char		ch;
	std::string		strTmp;
	if (m_filename.length() == 0)
		return FILENAMEEMPTY;
	if((fp = fopen(m_filename.c_str(), "r")) == NULL)
	{
		printf("file cannot be opened\n");
		return CONNOTOPENED;
	}
	while((ch = fgetc(fp)) != EOF)
	{	
		//fputc(ch, stdout);
		if ( '\r' == ch || '\n' == ch || 0 == ch || ' ' == ch)
		{
			if (strTmp.length())
			{
				m_sentences.push_back(strTmp);
				strTmp.clear();
				cleaningInvalid(fp);
			}
		}
		else
		{
			strTmp += ch;
		}
	}

	if (strTmp.length())
	{
		m_sentences.push_back(strTmp);
	}

	fclose(fp);
	return SUCCESS;
}
